{% extends 'base.html' %}
{% load staticfiles %}
{% load codalab_tags %}
{% load tz %}

{% block page_title %}Submissions Metadata{% endblock page_title %}
{% block head_title %}Submissions Metadata{% endblock %}

{% block content %}
<div class="submissions">
    <div class="well submission-phase-select">
        <div class="row">
            <div class="col-md-6">
                <h4>Phase</h4>
            </div>
            <div class="col-md-6">
                <a href="{% url "competitions:view" pk=competition.pk %}" class="pull-right" >Go Back to Competition Homepage</a>
            </div>
        </div>
        <select id="phase_selector" class="form-control">
            {% for phase in competition.phases.all %}
                <option {% if phase == selected_phase %}selected{% endif %} value="{{ phase.pk }}">{{ phase.label }} - {{ phase.start_date|utc }} UTC</option>
            {% endfor %}
        </select>
    </div>

    <h4>Submissions</h4>

    {% if submission_list %}
        <table class="table table-striped table-bordered">
            <thead>
                <tr style="text-align: center; font-weight: bold;">
                    <td>Submitted by</td>
                    <td width="150">Date</td>
                    <td>Compute worker</td>
                    <td>Docker image</td>
                    <td>Queue name</td>
                    <td>Submission ID#</td>
                    <td>Status</td>
                    <td>Prediction/Scoring?</td>
                    <td>Running processes (in temp dir)</td>
                    <td>Ingestion program duration</td>
                    <td>Start RAM</td>
                    <td>End RAM</td>
                    <td>Start CPU</td>
                    <td>End CPU</td>
                    <td>Start swap</td>
                    <td>End swap</td>
                </tr>
            </thead>
            <tbody>

                {% for submission in submission_list %}

                    {% for metadata in submission.metadatas.all %}
                        {% if metadata.submission.status.name == "Failed" %}
                            <tr class="failed-submission">
                        {% elif metadata.submission.status.name == "Finished" %}
                            <tr class="finished-submission">
                        {% else %}
                            <tr>
                        {% endif %}
                            <td>{{ metadata.submission.participant.user.username }}</td>
                            <td>{{ metadata.submission.submitted_at|date:"Y-m-d h:m" }}</td>
                            <td>{{ metadata.hostname }}</td>
                            <td>{{ submission.docker_image }}</td>
                            <td>{{ submission.queue_name }}</td>
                            <td>{{ metadata.submission.pk }}</td>
                            <td>{{ metadata.submission.status }}</td>
                            <td>{% if metadata.is_predict %}Prediction{% else %}Scoring{% endif %}</td>
                            <td>{{ metadata.simple.processes_running_in_temp_dir }}</td>
                            <td>{{ metadata.simple.ingestion_program_duration|floatformat:-2 }}</td>
                            <td>{{ metadata.simple.beginning_virtual_memory_usage }}</td>
                            <td>{{ metadata.simple.beginning_swap_memory_usage }}</td>
                            <td>{{ metadata.simple.beginning_cpu_usage }}</td>
                            <td>{{ metadata.simple.end_cpu_usage }}</td>
                            <td>{{ metadata.simple.end_virtual_memory_usage }}</td>
                            <td>{{ metadata.simple.end_swap_memory_usage }}</td>
                        </tr>
                    {% endfor %}
                {% endfor %}
            </tbody>
        </table>
        <nav aria-label="Page navigation example">
            <ul class="pagination">
                {% if submission_list.has_previous %}
                    <li><a href="?page=1{% if selected_phase %}&phase={{ selected_phase.pk }}{% endif %}">&laquo; first</a></li>
                    <li><a href="?page={{ submission_list.previous_page_number }}{% if selected_phase %}&phase={{ selected_phase.pk }}{% endif %}">previous</a></li>
                {% endif %}

                {% if submission_list.has_next %}
                    <li><a href="?page={{ submission_list.next_page_number }}{% if selected_phase %}&phase={{ selected_phase.pk }}{% endif %}">next</a></li>
                    <li><a href="?page={{ submission_list.paginator.num_pages }}{% if selected_phase %}&phase={{ selected_phase.pk }}{% endif %}">last &raquo;</a></li>
                {% endif %}
            </ul>
        </nav>
        Page {{ submission_list.number }} of {{ submission_list.paginator.num_pages }}.
    {% else %}
        <i>No submissions!</i>
    {% endif %}
</div>
{% endblock %}

{% block js %}
    $(function () {
        $("#phase_selector").change(function(){
            window.location = "/competitions/{{ competition.pk }}/submission_metadata/" + $(this).val();
        });
    });
{% endblock js %}
